package com.credithc.customers.manager.schedule;

import com.credithc.customers.manager.service.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 * @description: 统计账户总数定时任务统计
 * @author: fby
 * @date: 2019/5/13 10:14
 * @param: null
 * @return:
 */

@Slf4j
public class StatsAccountInfoByDateSysJob implements Job {

    @Autowired
    private ScheduleService scheduleService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            log.info("=========================================开始统计客户账户总数=========================================");
            JobDataMap dataMap = context.getMergedJobDataMap();
            String startDate = dataMap.getString("startDate");
            String endDate = dataMap.getString("endDate");
            String system = dataMap.getString("system");
            if (startDate != null && endDate != null) {
                Date start = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
                Date end = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
                Calendar dd = Calendar.getInstance();
                dd.setTime(start);
                while(dd.getTime().before(end)){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String doDate = sdf.format(dd.getTime());
                    scheduleService.statsAccountInfoSystem(doDate,system);
                    dd.add(Calendar.DATE, 1);
                }
                scheduleService.statsAccountInfoSystem(endDate,system);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
